const DEBUG = process.env.NODE_ENV !== 'production';

// const PAGES = {
//   index: {
//     entry: 'src/apps/index/main.js',
//     template: 'src/apps/index/public/index.html',
//     filename: 'index.html',
//     chunks: ['chunk-vendors', 'chunk-common', 'index']
//   },
//   app1: {
//     entry: 'src/apps/app1/main.js',
//     template: 'src/apps/app1/public/app1.html',
//     filename: 'app1.html',
//     chunks: ['chunk-vendors', 'chunk-common', 'app1']
//   },
//   app2: {
//     entry: 'src/apps/app2/main.js',
//     template: 'src/apps/app2/public/app2.html',
//     filename: 'app2.html',
//     chunks: ['chunk-vendors', 'chunk-common', 'app2']
//   }
//   ,
//   aaa: {
//     entry: 'src/apps/aaa/main.js',
//     template: 'src/apps/aaa/public/aaa.html',
//     filename: 'aaa.html',
//     chunks: ['chunk-vendors', 'chunk-common', 'aaa']
//   }
// }

let PAGES = {};
const modulesFile = require('./modules.json');
for(let key in modulesFile){
  PAGES[key] ={
      title: key,
      entry: `${modulesFile[key]}/main.js`,
      template: `${modulesFile[key]}/public/index.html`,
      filename: `index.html`,
      chunks: ['chunk-vendors', 'chunk-common', key]
  }
}

let _publicPath = './';

const path = require('path');


let page = {}
// let pageName = require('./build/config/serve_module').name;
// console.log('process.env', process.env)
// console.log('process.argv', process.argv)

//处理获取appName，规则：获取最后一个参数
let pageName = process.argv[process.argv.length-1] || 'index' // 获取build后面的参数确定执行哪个文件
// let pageName = process.argv.slice(4)[0]
// let pageName = 'app2'
console.info('======正在编译：%s 模块======', pageName);

// console.log('DEBUG', DEBUG)
let _outputDir = 'dist/'+pageName;
if (DEBUG) {
  // page = PAGES; //判断开发环境不用
  page[pageName] = PAGES[pageName];
  // _publicPath = _publicPath + pageName
  // _publicPath = '/';
  // _outputDir = path.join(__dirname,'dist')
  // _outputDir = '';
} else {
  //假如命令npm run build-index,
  //就会得到projectname=index
  page[pageName] = PAGES[pageName];
  // _publicPath = _publicPath + pageName;
}

// const glob = require("glob")
// let entries;
// try {
//     entries = glob('src/apps/*', {sync: true})
// } catch (err) {
//     entries = []
//     console.log('读取目录出错！')
//     throw err
// }
// console.log('entries', entries)

console.log('page', page)

console.log('_publicPath', _publicPath)
// console.log(((()=>{
//   let temp = []
//   for(let key in PAGES){
//     temp.push({
//       glob: '../src/apps/'+key+'/public/'+PAGES[key].filename,
//       matchBase: false
//     })
//   }
//   return ()...temp};
// })()))
module.exports = {
  publicPath: _publicPath,
  // publicPath: './dist/app1/',
  outputDir: _outputDir,
  // outputDir: 'dist/',
  assetsDir:'./',
  pages: page,
  // pages:{
  //   app1: {
  //         entry: 'src/apps/app1/main.js',
  //         template: 'src/apps/app1/public/app1.html',
  //         filename: 'app1.html',
  //         chunks: ['chunk-vendors', 'chunk-common', 'app1']
  //       }
  // },
  productionSourceMap: false,
  devServer: {
    open: true,
    host: '0.0.0.0',
    port: 1989,
    https: false,
    hotOnly: false,
    // contentBase: [path.join(__dirname, 'dist'), path.join(__dirname, 'src', 'apps', 'app1', 'public')],
    compress: true,
    openPage: `/index.html`,
    after: function(app, server){
      // console.log('1121312312',  path.join(__dirname, 'src', 'apps', 'app1'))
    }
  },
  chainWebpack: config => {
    // if (process.env.NODE_ENV === 'production') {
    //   // 为生产环境修改配置...
    // } else {
    //   // 为开发环境修改配置...
    // }
    
    //处理忽略文件，忽略业务模块中的入口模板，public/模块名称.html
    let ignores = []
    for(let key in page){
      ignores.push({
        glob: '../src/apps/'+key+'/public/'+page[key].filename,
        matchBase: false
      })
    }
    // 处理各个入口的静态文件copy
    config
      .plugin('copy')
        .tap(args => {
          let i = 0;
          for(var key in page){
            let item = page[key]
            // console.log(path.join(__dirname, item.entry.replace(/(.*\/)([A-Za-z]+)\.js/, '$1'),'public'))
            args[0].push({
              from: path.join(__dirname, item.entry.replace(/(.*\/)([A-Za-z]+)\.js/, '$1'),'public'),
              // to: args[0][0].to,
              to: path.join(__dirname,'dist', key),
              // toType: 'dir',
              ignore: [
                '.DS_Store',
                ...ignores
              ]
            })
            // console.log(args[0])
            i++;
          }
          
          return args;
        })
  }
}